package com.sevencar.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.sevencar.R;
import com.sevencar.util.FileUtils;
import com.umeng.analytics.MobclickAgent;

public class MainActivity extends FragmentActivity {

	protected static final String TAG = "MainActivity";

	private Button[] mTabs;
	private HomePageFragment homePageFragment;//��ҳҳ��
	private OrderManageFragment orderManageFragment;//
	private CommentManageFragment commentManageFragment;//
	private FeeFragment feeFragment;//�ҵĽ���
	private MineFragment mineFragment;//
	private Fragment[] fragments;
	private int index;
	private RelativeLayout[] tab_containers;
	// ��ǰfragment��index
	private int currentTabIndex;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		MobclickAgent.setDebugMode( true );

		FileUtils.copyAssetsDatabaseToData(this, "sevencarc.db");
		
		initView();
		
		
		homePageFragment = new HomePageFragment();
		orderManageFragment = new OrderManageFragment();
		
		commentManageFragment = new CommentManageFragment();
		mineFragment = new MineFragment();
		feeFragment = new FeeFragment();
		
		fragments = new Fragment[] { homePageFragment , orderManageFragment , commentManageFragment,feeFragment, mineFragment };
		
		getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, homePageFragment)
		.add(R.id.fragment_container, orderManageFragment).hide(orderManageFragment).show(homePageFragment)
		.commit();
	}

	/**
	 * ��ʼ�����
	 */
	private void initView() {
		mTabs = new Button[5];
		mTabs[0] = (Button) findViewById(R.id.btn_conversation_zaina);//���İ�ť
		mTabs[1] = (Button) findViewById(R.id.btn_gushi);//���°�ť
		mTabs[2] = (Button) findViewById(R.id.btn_conversation);//���лỰ��ť
		mTabs[3] = (Button) findViewById(R.id.btn_fee);//��ϵ�˰�ť
		mTabs[4] = (Button) findViewById(R.id.btn_address_list);//��ϵ�˰�ť
		mTabs[0].setSelected(true);

	}

	/**
	 * button����¼�
	 * 
	 * @param view
	 */
	public void onTabClicked(View view) {
		switch (view.getId()) {
		case R.id.btn_conversation_zaina:
			index = 0;
			break;
		case R.id.btn_gushi:
			index = 1;
			break;
		case R.id.btn_conversation:
			index = 2;
			break;
		case R.id.btn_fee:
			index = 3;
			break;
		case R.id.btn_address_list:
			index = 4;
			break;
		}
		if (currentTabIndex != index) {
			FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
			trx.hide(fragments[currentTabIndex]);
			if (!fragments[index].isAdded()) {
				trx.add(R.id.fragment_container, fragments[index]);
			}
			trx.show(fragments[index]).commit();
		}
		
		initdrawaletop();
		if(index == 0){
			Drawable drawableTop = getResources().getDrawable(R.drawable.menu_home1);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(index == 1){
			Drawable drawableTop = getResources().getDrawable(R.drawable.menu_mall1);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(index == 2){
			Drawable drawableTop = getResources().getDrawable(R.drawable.menu_gwc1);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(index == 3){
			Drawable drawableTop = getResources().getDrawable(R.drawable.menu_car1);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}else if(index == 4){
			Drawable drawableTop = getResources().getDrawable(R.drawable.menu_my1);
			mTabs[index].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);
		}
		
		mTabs[currentTabIndex].setSelected(false);
		mTabs[index].setSelected(true);
		currentTabIndex = index;
		
		
		
		
		
	}
	
	private void initdrawaletop(){
		
		Drawable drawableTop0 = getResources().getDrawable(R.drawable.menu_home);
		mTabs[0].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop0 , null, null);
		
		Drawable drawableTop1 = getResources().getDrawable(R.drawable.menu_mall);
		mTabs[1].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop1 , null, null);
		
		Drawable drawableTop2 = getResources().getDrawable(R.drawable.menu_gwc);
		mTabs[2].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop2 , null, null);
		
		Drawable drawableTop3 = getResources().getDrawable(R.drawable.menu_car);
		mTabs[3].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop3 , null, null);
		
		Drawable drawableTop4 = getResources().getDrawable(R.drawable.menu_my);
		mTabs[4].setCompoundDrawablesWithIntrinsicBounds(null, drawableTop4 , null, null);
		
	}
	
	

	
	 private long exitTime = 0;

	    @Override
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	        if (keyCode == KeyEvent.KEYCODE_BACK
	                && event.getAction() == KeyEvent.ACTION_DOWN) {
	            if ((System.currentTimeMillis() - exitTime) > 2000) {
	                Toast.makeText(getApplicationContext(), "再按一次退出",
	                        Toast.LENGTH_SHORT).show();
	                exitTime = System.currentTimeMillis();
	            } else {
//	                moveTaskToBack(false);
	            	MobclickAgent.onKillProcess(this);
	                finish();
	                
	                System.exit(0);
	            }
	            return true;
	        }
	        return super.onKeyDown(keyCode, event);
	    }	
	    
	    public void onResume() {
	        super.onResume();
	        MobclickAgent.onResume(this);       //统计时长
	    }
	    public void onPause() {
	        super.onPause();
	        MobclickAgent.onPause(this);
	    }
	
	
}